(defun random-in-range (min max)
    (+ (random (- max min)) min))

(defun get-window-width (window)
    (sdl2:get-window-size window))

(defun get-window-heigh (window)
    (second (multiple-value-list (sdl2:get-window-size window))))

(defun random-rect-motion! (rect win)
    (setf (sdl2:rect-x rect)
          (mod (+ (sdl2:rect-x rect)
                  (random-in-range -10 11))
               (get-window-width win)))
    
    (setf (sdl2:rect-y rect)
          (mod (+ (sdl2:rect-y rect)
                  (random-in-range -10 11))
               (get-window-heigh win))))

(defparameter *screen-width*  640)
(defparameter *screen-height* 480)

(defun brownian ()
        (sdl2:with-init (:video)
            (sdl2:with-window (win :title "Симуляция броуновского движения"
                                   :w *screen-width*
                                   :h *screen-height*
                                   :flags '(:shown :resizable))
                (let ((scr-sur    (sdl2:get-window-surface win))
                      (rect (sdl2:make-rect 100 100 64 64))
                      (lambda-img  (sdl2:load-bmp "/home/iam/prog/lisp/lambda.bmp")))
                    ;(print (multiple-value-list (sdl2:get-window-size win)))
                    (sdl2:with-event-loop ()
                        (:quit () t)
                        (:idle ()
                               (setf scr-sur (sdl2:get-window-surface win))
                               (sdl2:fill-rect scr-sur
                                               nil
                                               (sdl2:map-rgb (sdl2:surface-format scr-sur) 10 160 100))
                               
                               (sdl2:blit-surface lambda-img
                                                  nil
                                                  scr-sur                                       
                                                  rect)
                               (sdl2:update-window win)
                               
                               (random-rect-motion! rect win)))))))
